/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MinhNH;

import Entity.Account;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *

 */
public class MinhNHServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            HttpSession session = request.getSession(true);
            Context ctx = new InitialContext();
            MinhNHSessionBeanLocal MLocal = (MinhNHSessionBeanLocal) ctx.lookup("MinhNHSessionBeanLocalJNDI");
            if(action.equals("AdviewAccount"))
            {
                Account[] objA = MLocal.getAccount();
                session.setAttribute("ADVIEWACCOUNT", objA);
                request.getRequestDispatcher("ADManagerAccount.jsp").forward(request, response);
            }
            else if(action.equals("Lock"))
            {
                String user = request.getParameter("idLock");
                if(MLocal.LockAccount(user))
                {
                    request.getRequestDispatcher("MinhNHServlet?action=AdviewAccount").forward(request, response);
                }
            }
            else if(action.equals("unLock"))
            {
                String user = request.getParameter("idUnLock");
                if(MLocal.UnlockAccount(user))
                {
                    request.getRequestDispatcher("MinhNHServlet?action=AdviewAccount").forward(request, response);
                }
            }
            else if(action.equals("detailAcc"))
            {
                String user = request.getParameter("dtid");
                Account objA = MLocal.findAccountByPrimarykey(user);
                session.setAttribute("DetailAccount", objA);
                request.getRequestDispatcher("AdDetailAccount.jsp").forward(request, response);
            }
            else if(action.equals("Next"))
            {
                Account[] objAcc = MLocal.findEmailOfAccount(request.getParameter("txtEmail"));
                if(objAcc.length==0)
                {
                    String err="Email not found !";
                    session.setAttribute("Error", err);
                    request.getRequestDispatcher("Forgot1.jsp").forward(request, response);
                }
                else
                {
                    session.setAttribute("Email", objAcc);
                    request.getRequestDispatcher("Forgot2.jsp").forward(request, response);
                }
            }
            else if(action.equals("Change"))
            {
                if(MLocal.changePass(request.getParameter("txtUser"), request.getParameter("txtPass")))
                {
                    String err="Password changed successfully !!!<br/> You can login with new password";
                    session.setAttribute("mess", err);
                    request.getRequestDispatcher("ErrorUser.jsp").forward(request, response);
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
